import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { ElMessage } from 'element-plus'
import MyLogin from '../views/MyLogin.vue'
import HomeView from '../views/HomeView.vue'
import EchartsView from '../views/HomeChild/EchartsView.vue'
import PermissionList from '../views/HomeChild/PermissionList.vue'
import UserListShow from '../views/HomeChild/UserListShow.vue'
import UserMessage from  '../views/HomeChild/UserMessage.vue'
import UserUpdate from  '../views/HomeChild/UserUpdate.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: MyLogin,
  },
  {
    path: '/homeview',
    name: 'homeview',
    component: HomeView,
    redirect: '/echarts',
    children: [
      {
        path: '/echarts',
        name: 'echarts',
        component: EchartsView,
        meta: {
          title: '数据展示'
        }
      },
      {
        path: '/permission',
        name: 'permission',
        component: PermissionList,
        meta: {
          title: "权限列表"
        }
      },
      {
        path: '/userlist',
        name: 'userlist',
        component:UserListShow,
        redirect: '/userlist',
        meta: {
          title: "住户信息"
        },
        children: [
          {
            path: '/usermessage',
            name: 'usermessage',
            component:UserMessage,
            meta: {
              classifyTitle: "用户信息",//分类标题
              title: "住户列表"
            }
          },
          {
            path: '/userupdate',
            name: 'userupdate',
            component:UserUpdate,
            meta: {
              classifyTitle: "用户信息",//分类标题
              title: "住户信息修改"
            }
          },
        ]
      },
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//判断是否登录
router.beforeEach((to, from, next) => {
  console.log(to);
  if (to.path != '/login') {
    if (!localStorage.getItem('user')) {
      ElMessage({
        type: 'warning',
        message: '请先登陆！',
      })
      next('/login')
    }
  }
  next()
})

export default router
